package com.socialin.android.hackaton.fragment;

import com.socialin.android.hackaton.draw.R;
import com.socialin.android.hackaton.draw.drawing.DrawingView;
import com.socialin.android.hackaton.draw.listener.DoubleTapListener;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

public class UiFragment extends Fragment implements OnCheckedChangeListener, OnClickListener, DoubleTapListener {
	
	DrawingView drawingView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.ui, container, false);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		View v = getView();

		((ToggleButton) v.findViewById(R.id.brush_btn))
				.setOnCheckedChangeListener(this);
		((ToggleButton) v.findViewById(R.id.erase_btn))
				.setOnCheckedChangeListener(this);
		((ToggleButton) v.findViewById(R.id.friend_btn))
				.setOnCheckedChangeListener(this);
		((Button) v.findViewById(R.id.fullscreend_btn))
				.setOnClickListener(this);
		((ToggleButton) v.findViewById(R.id.sound_btn))
				.setOnCheckedChangeListener(this);
		((ToggleButton) v.findViewById(R.id.settings_btn))
				.setOnCheckedChangeListener(this);
		((ToggleButton) v.findViewById(R.id.private_btn))
				.setOnCheckedChangeListener(this);
		((ToggleButton) v.findViewById(R.id.video_btn))
				.setOnCheckedChangeListener(this);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		final int id = buttonView.getId();
		switch (id) {
		case R.id.brush_btn:
			drawingView.setDrawMode(DrawingView.DRAW_MODE_BRUSH);
			break;
		case R.id.erase_btn:
			drawingView.setDrawMode(DrawingView.DRAW_MODE_PUT_ICON);
			break;
		case R.id.friend_btn:
			drawingView.toggleDataInput();
			break;
		case R.id.fullscreend_btn:
			break;
		case R.id.sound_btn:
			break;
		case R.id.settings_btn:
			break;
		case R.id.private_btn:
			drawingView.toggleSyncing();
			break;
		case R.id.video_btn:
			break;
		}
	}

	public void setDrawingView(DrawingView drawingView) {
		this.drawingView = drawingView;
		drawingView.setDoubleTapListener(this);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == R.id.fullscreend_btn){
			Animation animToLeft = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_left);
			animToLeft.setFillAfter(true);
			Animation animToRight = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_right);
			animToRight.setFillAfter(true);
			getView().findViewById(R.id.right_panel).startAnimation(animToRight);
			getView().findViewById(R.id.left_panel).startAnimation(animToLeft);
		}
	}
	
	private void showUi(){
		Animation animToLeft = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_left2);
		Animation animToRight = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_right2);
		
		getView().findViewById(R.id.right_panel).startAnimation(animToLeft);
		getView().findViewById(R.id.left_panel).startAnimation(animToRight);
	}

	@Override
	public void onDoubleTap() {
		showUi();
	}

}
